<template>
	<view class="wrapper">
		<view class="top">
			<view class="image"><image src="../../static/image/logo.png" mode=""></image></view>
			<view class="text">
				海外货运
			</view>
		</view>
		<view class="btn" @click="tologin">
			<button>微信用户一键登陆</button>
		</view>
		<view class="bottom">
			<image :src="image" mode="" @click="changeArgee"></image>
			<view class="text1">
				您已阅读并同意
			</view>
			<view class="text2" @click="toAgreement">
				《用户协议与交易服务协议》	
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image:"../../static/image/gou-5@2x.png",
				isArgee:true,
				userInfo:{}
			}
		},
		methods: {
			changeArgee(){
				if(!this.isArgee){
					this.image="../../static/image/gou-5@2x.png"
					this.isArgee=true
				}else{
					this.image="../../static/image/tuoyuan.png"
					this.isArgee=false
				}
			},
			async getUserInfo(){
				let token=uni.getStorageSync("token")
				const {data}=await this.$http({
					url:"member/info",
					header:{
						token
					}
				})
				return data.data
			},

			tologin(){
				if(!this.isArgee){
					uni.showToast({
						title:"请先阅读并同意《用户协议与交易服务协议》",
						icon:"none"
					})
					return
				}
				uni.getUserProfile({
					desc:"获取用户信息",
				    success:(infoRes)=> {
						let nickName=this.userInfo.nickName=infoRes.userInfo.nickName;
						let avatarUrl=this.userInfo.avatarUrl=infoRes.userInfo.avatarUrl;
						uni.login({
							success:async (res)=> {
								let code=res.code;
								let {openid}=await this.getOpenId(code)
								console.log(openid)
								const result=await this.login(nickName,avatarUrl,openid)
								// const result=await this.login("SHENG","/uploads/20211115/1b4a16f7c74c2f99262731ab3c948eb9.png","132")
								// const result=await this.login("测试","/uploads/20211103/5679b71f4a9ddbfc09b57632c523c0b3.jpg","123456")
								// const result=await this.login("迪拜员工","/assets/img/qrcode.png","db23141414")
								console.log(result)
								if(result.code==0){
									uni.showToast({
										title:"登陆失败",
										icon:"error"
									})
									return
								}
								let app=getApp()
								app.globalData.islogin=true
								uni.setStorageSync("token",result.data.token);
								uni.showToast({
									title:"登陆成功"
								})
								let userInfo=await this.getUserInfo()
								uni.setStorageSync("userInfo",userInfo)
								console.log(userInfo)
								if(userInfo.lden=="staff_cn"){
									uni.redirectTo({
										url:"/pages/inemployees/waitTohouse/waitTohouse"
									})
									return
								}
								if(userInfo.lden=="staff_db"){
									uni.redirectTo({
										url:"/pages/outemployees/roading/roading"
									})
									return
								}
								uni.switchTab({
									url:"/pages/index/index"
								})
								console.log(uni.getStorageSync("userInfo"))
							}
						})
				    },
					fail(res) {
						console.log(res)
					}
				})
			},
			async getOpenId(code){
				const {data}=await this.$http({
					url:"member/getopenid?code="+code
				})
				return data.data
				
			},
			async login(nickName,avatarUrl,openid){
				const {data}=await this.$http({
					url:"member/login",
					methods:"post",
					data:{
						"nickname":nickName,
						"avatar":avatarUrl,
						"openid":openid
					}
				})
				console.log(data)
				return data;
			},
			toAgreement(){
				uni.navigateTo({
					url:"/pages/agreement/agreement"
				})
			}
		}
	}
</script>

<style scoped>
.wrapper{
	position: relative;
	height: 100vh;
}
.top{
	padding-top: 150rpx;
	text-align: center;
}
.top image{
	width: 160rpx;
	height: 160rpx;
}
.top .text{
	font-size: 36rpx;
	font-family: PingFang SC;
	font-weight: 600;
	color: #333333;
}
.btn{
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%,-50%);
}
.btn button{
	width: 658rpx;
	height: 88rpx;
	background: #F7CC46;
	border-radius: 44rpx;
	font-size: 35rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #FFFFFF;
}
.bottom .circle{
	width: 31rpx;
	height: 31rpx;
	background: #FFFFFF;
	border: 1rpx solid #999999;
	border-radius: 50%;
}
.bottom image{
	width: 31rpx;
	height: 31rpx;
}
.bottom{
	position: absolute;
	bottom: 5%;
	left:50%;
	transform: translate(-50%);
	width: 600rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}
.bottom .text1{
	font-size: 24rpx;
	font-family: PingFang SC;
	font-weight: 600;
	color: #333333;
	margin-left: 10rpx;
}
.bottom .text2{
	font-size: 24rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #F7CC46;
}
</style>
